#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/replace.hpp>
#include "log.h"

std::string get_filecontent(const std::string &filename, bool showmsg)
{
    std::string ret;
    std::ifstream in(filename, std::ios::in | std::ios::binary | std::ios::ate);

    if (in.is_open() == false && showmsg)
    {
        ERR("open file failed! filename={}", filename);
        return "";
    }

    in.seekg(0, std::ios::end);
    ret.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read((char*)ret.data(), ret.size());
    in.close();
    return std::move(ret);
}

static bool check_filename(const std::string &filename, const std::vector<std::string> &extension)
{
    for (auto it = extension.begin(); it != extension.end(); ++it)
    {
        int index = (int)filename.find(*it);

        if (index > 0 && index + (*it).size() == filename.size())
        {
            return true;
        }
    }

    return false;
}

void get_filenames(
    const std::string &path,
    const std::vector<std::string> &extension,
    std::vector<std::string> &out,
    bool only_name)
{
    namespace fs = boost::filesystem;
    fs::path full_path = fs::system_complete(fs::path(path));
    unsigned long err_count = 0;

    if (!fs::exists(full_path))
    {
        ERR("can't find dir: %s", full_path.string().c_str());
        return;
    }

    if (fs::is_directory(full_path))
    {
        fs::directory_iterator end_iter;

        for (fs::directory_iterator dir_itr(full_path); dir_itr != end_iter; ++dir_itr)
        {
            try
            {
                if (fs::is_directory(*dir_itr))
                {
                    std::string tmppath = dir_itr->path().string();

                    if (tmppath == ".svn" || tmppath == ".vs" || tmppath == ".git")
                    {
                        continue;
                    }

                    get_filenames(tmppath, extension, out, only_name);
                }
                else
                {
                    std::string strFileName = dir_itr->path().string();

                    if (check_filename(strFileName, extension) == false)
                    {
                        continue;
                    }

                    if (only_name == false)
                    {
                        fs::path full_file = fs::system_complete(fs::path(strFileName));
                        std::string f = full_file.string();
                        boost::algorithm::replace_all(f, "\\", "/");
                        out.push_back(f);
                    }
                    else
                    {
                        boost::algorithm::replace_all(strFileName, "\\", "/");
                        strFileName = strFileName.substr(strFileName.rfind("/") + 1);

                        for (auto it_ext = extension.begin(); it_ext != extension.end(); it_ext++)
                        {
                            boost::algorithm::replace_all(strFileName, *it_ext, "");
                        }

                        out.push_back(strFileName);
                    }
                }
            }
            catch (const std::exception & ex)
            {
                ++err_count;
                ERR("get_filenames error. error = %s", ex.what());
            }
        }
    }
}